package com.ml.framework.base.action;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @filename      : BaseAction.java
 * @description   : TODO(一句话描述该类做什么)
 * @author        : wanghongkai
 * @create        : 2013-7-3 下午2:16:36
 * @copyright     : hileto Corporation 2013
 *
 * Modification History:
 * Date             Author       Version
 * --------------------------------------
 * d2013-7-3 下午2:16:36
 */
public class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware
{

	private static final long serialVersionUID = 1L;
	
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	
	

	@Override
	public String execute() throws Exception
	{
		//这里默认返回success，就会默认去对应页面
		return Action.SUCCESS;
	}

	@Override
	public void setServletResponse(HttpServletResponse response)
	{
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request)
	{
		this.request = request;
	}
	
	/**
	 * 返回Map封装的请求参数
	 * 
	 * @return
	 */
	protected Map<String, String> getParamMap()
	{
		Map<String, String[]> m = this.request.getParameterMap();
		if (m == null || m.isEmpty())
			return null;

		Map<String, String> rm = new HashMap<String, String>();
		Iterator<Entry<String, String[]>> it = m.entrySet().iterator();
		while (it.hasNext())
		{
			Entry<String, String[]> e = it.next();
			String v = e.getValue()[0];
			rm.put(e.getKey(), v = v == null ? null : v.length() == 0 ? null : v);
		}
		return rm;
	}
}
